package com.ruoyi.project.gxgw_modules.output.controller;


import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.gxgw_modules.goods.service.IGoodsService;
import com.ruoyi.project.gxgw_modules.output.domain.Output;
import com.ruoyi.project.gxgw_modules.output.outputVo.OutputVo;
import com.ruoyi.project.gxgw_modules.output.service.IOutputService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * 商品出库 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-01-21
 */
@Controller
@RequestMapping("/gxgw_modules/output")
public class OutputController extends BaseController {
	private String prefix = "gxgw_modules/output";

	@Autowired
	private IOutputService outputService;

	@Autowired
	private IGoodsService goodsService;

	@RequiresPermissions("gxgw_modules:output:view")
	@GetMapping()
	public String output() {
		return prefix + "/output";
	}

	/**
	 * 查询商品出库列表
	 */
	@RequiresPermissions("gxgw_modules:output:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Output output) {
		startPage();
		List<OutputVo> list = outputService.selectOutputList(output);
		return getDataTable(list);
	}

	/**
	 * 导出商品出库列表
	 */
	@RequiresPermissions("gxgw_modules:output:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(OutputVo output) {
		List<OutputVo> list = outputService.selectOutputList(output);
		ExcelUtil<OutputVo> util = new ExcelUtil<OutputVo>(OutputVo.class);
		return util.exportExcel(list, "output");
	}

	/**
	 * 新增商品出库
	 *//*
		 * @GetMapping("/add") public String add() { return prefix + "/add"; }
		 */
	/*
		*/
	/**
	 * 新增保存商品出库
	 *//*
		 * 
		 * @RequiresPermissions("gxgw_modules:output:add")
		 * 
		 * @Log(title = "商品出库", businessType = BusinessType.INSERT)
		 * 
		 * @PostMapping("/add")
		 * 
		 * @ResponseBody public AjaxResult addSave(Output output) { Date date = new
		 * Date(); Timestamp timeStamp = new Timestamp(date.getTime());
		 * output.setOutputTime(timeStamp); output.setAdminId(1); return
		 * toAjax(outputService.insertOutput(output)); }
		 */

	/**
	 * 修改商品出库
	 */
	@GetMapping("/edit/{outputId}")
	public String edit(@PathVariable("outputId") Integer outputId, ModelMap mmap) {
		Output output = outputService.selectOutputById(outputId);
	    
	    Timestamp timeStamp = new Timestamp(output.getOutputTime().getTime()); 
	    
	    output.setOutputTime(timeStamp);
		mmap.put("output", output);
		return prefix + "/edit";
	}

	/**
	 * 修改保存商品出库
	 */
	@RequiresPermissions("gxgw_modules:output:edit")
	@Log(title = "商品出库", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Output output) {
		int judge = outputService.selectCheckState(output.getOutputId());
		// 如果前端选择的状态为审核通过
		if (output.getCheckState() == 1) {

			// 检查之前是否通过审核，如果是，则不更新数目
			if (judge == 1) {
				return error(1, "审核已通过！禁止重复审核");
			}
			// 检查库存
			int total = goodsService.selectNum(output.getGoodsId());
            //库存足够则减少数量，不足则报错
			if (total >= output.getOutputNum())
				goodsService.reduceNum(output.getGoodsId(), output.getOutputNum());
			else
				return error(1, "该商品库存不足！");

		}
		// 如果前端选择的状态不是审核通过
		else {
			// 检查之前是否通过审核，如果是，则不允许修改审核的状态
			if (judge == 1) {
				return error(1, "审核已通过！禁止重复审核");
			}
		}
		
       //更新出库审核日期
		Date date = new Date();
		Timestamp timeStamp = new Timestamp(date.getTime());
		output.setCheckTime(timeStamp);
		// 设置修改人
		output.setCheckName(ShiroUtils.getSysUser().getUserName());
		return toAjax(outputService.updateOutput(output));
	}

	/**
	 * 删除商品出库
	 */
	@RequiresPermissions("gxgw_modules:output:remove")
	@Log(title = "商品出库", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(outputService.deleteOutputByIds(ids));
	}

}
